<script setup lang="ts">
import { inject, ref, provide, watch, onMounted } from 'vue';
import Menu from '../components/Menu.vue';
import type { MenuItem } from '../types/types.d.ts';
import Usercard from './Usercard.vue';
import Signup from './Signup.vue';
import Login from './Login.vue';
import FindPass from './FindPass.vue';
import { userinfo } from '@/stores/counter';
import { useRouter } from 'vue-router';
import uselogout from '@/api/getLogout';

const user = userinfo();
const menuDatas = inject('menuDatas') as MenuItem[];
const router = useRouter();
const avatar = ref();
const headerAvatar = ref();

const { logout } = uselogout();

const confirm = ref(false);

const onUserLogout = () => {
  confirm.value = true;
};

provide('onUserLogout', onUserLogout as () => void);

const confirmLogout = () => {
  logout();
  confirm.value = false;
};

const jumpHome = () => {
  router.push('/');
};

function onShow() {
  user.showUser = !user.showUser;
}

watch(
  () => user.showUser,
  async (newQuestion, oldQuestion) => {
    if (newQuestion) {
      avatar.value.style.height = '70px';
      avatar.value.style.width = '70px';
      avatar.value.style.marginTop = '20px';
      headerAvatar.value.style.marginRight = 'calc(20% - 10px)';
    } else {
      avatar.value.style.height = '50px';
      avatar.value.style.width = '50px';
      avatar.value.style.marginTop = '0px';
      headerAvatar.value.style.marginRight = '20%';
    }
  }
);

onMounted(() => {
  console.log('wew');
  user.showUser = false;
});
</script>

<template>
  <div class="header">
    <el-dialog v-model="confirm" width="30%">
      <span>你正在<b>退出登录</b>，请确认你的操作</span>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="confirm = false">取消</el-button>
          <el-button class="confirm-btn" @click="confirmLogout">
            确认退出
          </el-button>
        </span>
      </template>
    </el-dialog>
    <div class="header-title" @click="jumpHome">
      <el-text class="color-orange font-title font-xbs">XJTU</el-text>
      <el-text class="color-black font-title font-xbs">集约化网站</el-text>
    </div>
    <div class="flex-1" />
    <div class="header-avatar" id="userInfo" ref="headerAvatar">
      <div
        class="avatar"
        @click="onShow()"
        :class="{ 'yes-login': user.login, 'not-login': !user.login }"
        :style="{
          backgroundImage: user.login
            ? `url(${user.baseInfo.avator})`
            : `url(/assets/imgs/defaultAvatar.png)`,
        }"
        ref="avatar"
      ></div>
      <div class="color-orange user-name">{{ user.baseInfo.username }}</div>
      <Usercard class="usercard" v-if="user.login && user.showUser" />
      <Login
        class="login"
        v-if="!user.login && user.usercardPage == 0 && user.showUser"
      />
      <Signup
        class="signup"
        v-if="!user.login && user.usercardPage == 1 && user.showUser"
      />
      <FindPass
        class="findPass"
        v-if="!user.login && user.usercardPage == 2 && user.showUser"
      />
    </div>
  </div>
  <div class="header-menu">
    <Menu :data="menuDatas" />
  </div>
</template>

<style scoped>
.confirm-btn {
  background-color: #f5900a;
  color: white;
}

.header-menu {
  display: flex;
  width: 100%;
  justify-content: center;
  align-items: center;
}

.user-name {
  font-size: 16px;
  margin-top: 2px;
  z-index: 999;
}

.header {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100px;
  box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.2);
}

.header-title {
  margin-left: 70px;
  cursor: pointer;
}

.header-avatar {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  margin-right: 20%;
  z-index: 999;
  transition: margin-right 0.3s ease;
  /* background-color: pink; */
}

/* .header-avatar:hover>.avatar {
    width: 70px;
    height: 70px;
    margin-top: 10px;
} */

/* .header-avatar:hover {
    margin-right: calc(20% - 10px);
} */

/* .header-avatar:hover > .user-name {
    font-size: 20px;
} */

.font-title {
  font-size: 30px;
  font-weight: 400;
  margin: 0;
  padding: 0;
}

.avatar {
  background-position: center;
  background-repeat: no-repeat;
  background-size: contain;
  width: 50px;
  height: 50px;
  border-radius: 100%;
  z-index: 999;
  transition: width 0.3s ease, height 0.3s ease, margin-top 0.3s ease;
  cursor: pointer;
}

.yes-login {
  background-image: url(/assets/imgs/avatar.png);
}

.not-login {
  background-image: url(/assets/imgs/defaultAvatar.png);
}

.color-orange {
  color: #f5900a;
}

.color-black {
  color: black;
}

.findPass,
.usercard,
.signup,
.login {
  position: absolute;
  top: 60px;
  display: flex;
}
</style>
